/*
 * Copyright 2014 trinea.cn All right reserved. This software is the confidential and proprietary information of
 * trinea.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with trinea.cn.
 */
package com.pdstudio.carrecom.ui.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.logger.Logger;
import com.pdstudio.carrecom.ui.activity.ActivityMain;
import com.pdstudio.carrecom.ui.activity.ActivityWebView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
 * ImagePagerAdapter
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class ImagePagerAdapter extends RecyclingPagerAdapter {

	private Context context;
	private List<String> imageIdList;

	private int size;
	private boolean isInfiniteLoop;

	public ImagePagerAdapter(Context context, List<String> imageIdList) {
		this.context = context;
		this.imageIdList = imageIdList;
		this.size = imageIdList.size();
		isInfiniteLoop = false;
	}

	@Override
	public int getCount() {
		// Infinite loop
		return isInfiniteLoop ? Integer.MAX_VALUE : imageIdList.size();
	}

	/**
	 * get really position
	 * 
	 * @param position
	 * @return
	 */
	private int getPosition(int position) {
		return isInfiniteLoop ? position % size : position;
	}

	@Override
	public View getView(int position, View view, ViewGroup container) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = holder.imageView = new ImageView(context);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		holder.imageView.setScaleType(ScaleType.FIT_XY);
		
		//TODO:待删除
		
		//switch (position) {
		//case 1:
		//	holder.imageView.setImageResource(R.drawable.benz);
		//	break;
		//case 2:
		//	holder.imageView.setImageResource(R.drawable.benz21);
		//	break;
		//case 3:
		//	holder.imageView.setImageResource(R.drawable.benz3);
		//	break;	default:
		//	break;
		//}
		


		DisplayImageOptions options = new DisplayImageOptions.Builder()
		// .showImageOnLoading(R.drawable.ic_launcher) // 设置图片在下载期间显示的图片
		// .showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片
		// .showImageOnFail(R.drawable.ic_launcher) // 设置图片加载/解码过程中错误时候显示的图片
		// .displayer(new FadeInBitmapDisplayer(1000)) // 渐变动画
				.handler(new Handler()) // default
				.build();
		// Logger.i(mAdUrl);
		ImageLoader.getInstance().displayImage(
				imageIdList.get(getPosition(position)), holder.imageView,
				options);
		
		
		return view;
	}

	public Bitmap returnBitMap(String url) {
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	private static class ViewHolder {

		ImageView imageView;
	}

	/**
	 * @return the isInfiniteLoop
	 */
	public boolean isInfiniteLoop() {
		return isInfiniteLoop;
	}

	/**
	 * @param isInfiniteLoop
	 *            the isInfiniteLoop to set
	 */
	public ImagePagerAdapter setInfiniteLoop(boolean isInfiniteLoop) {
		this.isInfiniteLoop = isInfiniteLoop;
		return this;
	}
	
	@Override
	public Object instantiateItem(View container, int position) {
		Intent intent = new Intent();
		intent.setClass(context, ActivityWebView.class);
		Bundle bundle = new Bundle();
		bundle.putString("url", "http://www.baidu.com");
		intent.putExtras(bundle);
		ActivityMain.mactivity.startActivity(intent);
		return super.instantiateItem(container, position);
	}
	
}
